package com.example.controller;


import com.example.common.Result;
import com.example.model.bo.LoginInfo;
import com.example.service.LoginService;
import com.example.service.impl.LoginServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;
import java.util.Objects;

@CrossOrigin
@RestController
public class LoginController {

    @Autowired
    private LoginService loginService;

    /**
     * 用户注册
     * @param dto 注册信息
     * @return
     * @throws Exception
     */
    @PostMapping("/register")
    public Result register(@RequestBody LoginInfo dto) throws Exception {
        //调用Service实现账户注册
        Map<String, String> register_resp = loginService.register(dto);
        if (Objects.equals(register_resp.get("state"), "success")){
            return Result.success(register_resp.get("address"));
        }
        return Result.error(register_resp.get("state"));
    }

    /**
     * 用户登录
     * @param dto 登录信息
     * @return
     * @throws Exception
     */
    @PostMapping("/login")
    public Result login(@RequestBody LoginInfo dto) throws Exception {
        //调用Service实现账户登录
        Map<String, String> login_resp = loginService.login(dto);

        if (Objects.equals(login_resp.get("state"), "success")){
            return Result.success(login_resp.get("address"));
        }
        //打印登录状态
        System.out.println(login_resp.get("state")+" dsadasdsadasdddddddasdsdasd");
        return Result.error(login_resp.get("state"));
    }
}
